package com.leyou.item.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.BrandVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName: BrandService
 * @Description: 品牌接口
 * @Author: sunt
 * @Date: 2019/11/11 11:55
 * @Version 1.0
 **/
@Service
@Slf4j
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;


    /**
     * 品牌按条件分页查询
     * @param pageNum 当前页码
     * @param pageSize 每页展示条数
     * @param sortBy 排序字段
     * @param desc 是否为降序
     * @param key 搜索关键字
     * @return
     */
    public PageResult<Brand> queryBrandByPageAndSort(Integer pageNum, Integer pageSize, String sortBy, Boolean desc, String key) {
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        //条件过滤
        Example example = new Example(Brand.class);
        if(StrUtil.isNotBlank(key)) {//关键字搜索
            example.createCriteria().andLike("name","%" + key + "%").orEqualTo("letter",key);
        }
        if(StrUtil.isNotBlank(sortBy)) {//排序字段，注意空格
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        //执行分页查询
        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
        //返回查询结果
        return new PageResult<>(pageInfo.getTotal(),pageInfo);
    }

    /**
     * <li>1.保存品牌信息</li>
     * <li>2.保存品牌和商品分类关联信息</li>
     * @param brand 品牌信息
     * @param cids 类别信息
     */
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
        log.info("insert brand runing...");
        int insertResCount = brandMapper.insert(brand);
        if(insertResCount == 0) {
            throw new LyException(ExceptionEnum.BRAND_CREATE_FAILED);
        }
        log.info("insert brand category runing...");
        for (Long cid : cids) {
            int insertCount = brandMapper.insertCategoryBrand(cid, brand.getId());
            if(insertCount == 0) {
                throw new LyException(ExceptionEnum.BRAND_CREATE_FAILED);
            }

        }
    }

    /**
     * 更新品牌
     * 涉及到中间表品牌与分类的维护，先删除再添加
     * @param brandVo
     */
    @Transactional
    public void updateBrand(BrandVo brandVo) {
        log.info("update brand running...");
        //构造品牌信息入参
        Brand brand = new Brand();
        brand.setId(brandVo.getId());
        brand.setName(brandVo.getName());
        brand.setLetter(brandVo.getLetter());
        brand.setImage(brandVo.getImage());
        int resultCount = brandMapper.updateByPrimaryKey(brand);
        if(resultCount == 0) {
            throw new LyException(ExceptionEnum.UPDATE_BRAND_FAILED);
        }
        //删除关联关系
        log.info("delete brand category running...");
        int deleteResCount = brandMapper.deleteCategoryBrandByBid(brand.getId());
        if(deleteResCount == 0) {
            throw new LyException(ExceptionEnum.UPDATE_BRAND_FAILED);
        }
        //重新添加关联关系
        List<Long> cids = brandVo.getCids();
        log.info("insert brand category runniong....");
        for (Long cid : cids) {
            int insertResCount = brandMapper.insertCategoryBrand(cid, brandVo.getId());
            if(insertResCount == 0) {
                throw new LyException(ExceptionEnum.UPDATE_BRAND_FAILED);
            }
        }
    }
}
